package com.shuttle.tware.anexter.util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

/*
 * FileUtil is a file tool, so, can do file copy and file exists, dir exists
 * (c) Tody 2012, T-ware Inc.
 * Version: 1.0.0
 * 
 * Author: Tody Guo
 * 
 * */

public class fileUtil
{
	private static String TAG = "FileUtil";
	
	
	public static boolean copyFile(String from, String to, Boolean rewrite )
	{
		File fromFile = new File(from);
		File toFile = new File(to);
		
		if (!fromFile.exists()) 
		{
			Log.e(TAG, "not exists");
			return false;
		}
		
		if (!fromFile.isFile() || !fromFile.canRead()) 
		{
			Log.e(TAG, "isFile and canRead");
			return false;
		}
		
		if (!toFile.getParentFile().exists()) 
		{
			if(toFile.getParentFile().mkdirs())
			{
				Log.e(TAG, "mkdirs failed");
			}
		}
		
		if (toFile.exists() && rewrite) 
		{
			toFile.delete();
		}		
		try {

			java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);
			java.io.FileOutputStream fosto = new FileOutputStream(toFile);
			byte bt[] = new byte[1024];
			int c;
			
			while ((c = fosfrom.read(bt)) > 0) 
			{
				fosto.write(bt, 0, c);
			}
				
			fosfrom.close();
			fosto.close();
		} catch (Exception ex) 
		{
			Log.e(TAG, ex.getMessage());
			return false;
		}
		return true;
	}
	
	public static boolean fileExists(String path)
	{
		File f = new File(path);
		if(f.exists() && f.isFile())
		{
			return true;
		}
		
		return false;
	}
	
	public static boolean dirExists(String path)
	{
		File f = new File(path);
		if(f.exists() && f.isDirectory())
		{
			return true;
		}
		return false;
	}
	
	public static boolean deleteAFile(String path)
	{
		File f = new File(path);
		if(f.exists() && f.isFile())
		{
			return true;
		}
		
		if(!f.canWrite())
		{
			return false;
		}
		else
		{
			if(!f.delete())
			{
				Log.d("FileUtil", "Delete " + path + " failed.");
			}
		}
		return false;
	}
	
	public static void deleteFiles(String path, String suffix) throws SecurityException
	{

		File f = new File(path);
		
		if (!f.exists())
		{
			Log.e(TAG, path + " not found!");
			return ;
		}
		
		File[] fl =  f.listFiles();
		
		if (null == fl)
		{
			return ;
		}
				
		for (int i = 0;  i< fl.length; i++)
		{
			if (fl[i].isDirectory())
			{
				deleteFiles(fl[i].getAbsolutePath(), suffix);					
			}
			
			if(fl[i].toString().toLowerCase().endsWith(suffix))
			{
				Log.d(TAG, fl[i].toString());
				fl[i].delete();
			}
		}
}
	
	public static List<String> findFile(String path, String suffix)
	{
		List<String> list = new ArrayList<String>();
		
    	File f = new File(path);
    	if (!f.exists()) return list;

    	try{
    		File[] fl = f.listFiles();
    	
    		for (int i = 0; i< fl.length; i++)
    		{
    			Log.e(TAG, fl[i].toString());
    			if (fl[i].isDirectory())  
    			{
    				findFile(fl[i].getAbsolutePath(), suffix);
    			}else if (	fl[i].getName().toString().endsWith(suffix) )
    			{    			
    				Log.i(TAG, "Found " + fl[i].toString());
    				list.add(fl[i].toString());
    			}
    		}
    		return list;
    	}
    	catch (Exception e)
    	{
    		return list;
    	}

	}
	
}